/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.library.cards.topcolored;

import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.cards.ResourceTable;
import it.gmariotti.cardslib.library.cards.base.BaseMaterialCard;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

import static ohos.agp.components.Component.HIDE;
import static ohos.agp.components.Component.VISIBLE;

/**
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class TopColoredCard extends BaseMaterialCard {

    /**
     * Resource Background Color ID
     */
    protected
    int mColorResourceId;

    /**
     * Title to use for the title over the color
     */
    protected CharSequence mTitleOverColor;

    /**
     * Resource Id to use for the title over the color
     */
    protected
    int mTitleOverColorResId;

    /**
     * Title to use for the subtitle over the color
     */
    protected CharSequence mSubTitleOverColor;

    /**
     * Resource Id to use for the subtitle over the color
     */
    protected
    int mSubTitleOverColorResId;

    /**
     * An identifier for the layout resource to inflate when the ViewStub becomes visible
     */
    private int mSecondHalfViewStubLayoutId;


    private OnSetupInnerElements mOnSetupInnerElements;

    /**
     * Callback to setup inner Elements
     */
    public interface OnSetupInnerElements {
        void setupInnerViewElementsSecondHalf(Component secondHalfView);
    }

    // -------------------------------------------------------------
    // Constructors
    // -------------------------------------------------------------

    public TopColoredCard(Context context) {
        this(context, ResourceTable.Layout_native_material_topcolored_inner_base_main);
    }

    public TopColoredCard(Context context, int innerLayout) {
        super(context, innerLayout);
    }

    // -------------------------------------------------------------
    // Builder
    // -------------------------------------------------------------

    public static SetupWizard with(Context context) {
        return new SetupWizard(context);
    }

    public static final class SetupWizard {
        private final Context mContext;
        protected int mColorResourceId;
        protected CharSequence mTitleOverColor;
        protected int mTitleOverColorResId;
        protected CharSequence mSubTitleOverColor;
        protected int mSubTitleOverColorResId;
        private int mSecondHalfViewStubLayoutId;
        private OnSetupInnerElements mOnSetupInnerElements;

        private SetupWizard(Context context) {
            mContext = context;
        }

        public SetupWizard setColorResId(int colorId) {
            mColorResourceId = colorId;
            return this;
        }

        public SetupWizard setTitleOverColor(CharSequence textOverColor) {
            mTitleOverColor = textOverColor;
            return this;
        }

        public SetupWizard setTitleOverColor(int textOverColorResId) {
            mTitleOverColorResId = textOverColorResId;
            return this;
        }

        public SetupWizard setSubTitleOverColor(CharSequence subtitleOverColor) {
            mSubTitleOverColor = subtitleOverColor;
            return this;
        }

        public SetupWizard setSubTitleOverColor(int subtitleOverColorResId) {
            mSubTitleOverColorResId = subtitleOverColorResId;
            return this;
        }

        public SetupWizard setupSubLayoutId(int layoutId) {
            mSecondHalfViewStubLayoutId = layoutId;
            return this;
        }

        public SetupWizard setupInnerElements(OnSetupInnerElements innerElements) {
            mOnSetupInnerElements = innerElements;
            return this;
        }

        public TopColoredCard build() {
            TopColoredCard card = new TopColoredCard(mContext);
            card.setColorResourceId(mColorResourceId);
            card.setTitleOverColor(mTitleOverColor);
            card.setTitleOverColorResId(mTitleOverColorResId);
            card.setSubTitleOverColor(mSubTitleOverColor);
            card.setSubTitleOverColorResId(mSubTitleOverColorResId);
            card.setSecondHalfViewStubLayoutId(mSecondHalfViewStubLayoutId);
            card.setOnSetupInnerElements(mOnSetupInnerElements);
            card.build();
            return card;
        }
    }

    // -------------------------------------------------------------
    // Build
    // -------------------------------------------------------------

    @Override
    public void build() {


    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {
        //Use the title in super method
        super.setupInnerViewElements(parent, view);

        //Add a simple subtitle
        if (view != null) {

            ViewHolder holder = (ViewHolder) view.getTag();
            ComponentContainer viewStub = null;
            if (holder == null) {
                holder = new ViewHolder();
                holder.mLinearLayoutContainer = (DirectionalLayout) view.findComponentById(ResourceTable.Id_card_main_inner_topcolored_layout);
                holder.mTitleView = (Text) view.findComponentById(ResourceTable.Id_card_main_inner_topcolored_title);
                holder.mSubTitleView = (Text) view.findComponentById(ResourceTable.Id_card_main_inner_topcolored_subtitle);
                viewStub = (ComponentContainer) ((Component) getCardView()).findComponentById(ResourceTable.Id_card_halfcolored_secondhalf);
                if (viewStub != null) {
                    viewStub.addComponent(LayoutScatter.getInstance(getContext()).parse(mSecondHalfViewStubLayoutId, null, false));
                }
                view.setTag(holder);
            }

            //Color the LinearLayout
            if (holder.mLinearLayoutContainer != null && mColorResourceId != 0) {
                try {
                    ShapeElement element = new ShapeElement();
                    element.setRgbColor(new RgbColor(mContext.getResourceManager().getElement(mColorResourceId).getColor()));
                    holder.mLinearLayoutContainer.setBackground(element);
                } catch (Exception e) {
                    HiLog.error(Constants.LABEL_LOG, e.toString());
                }
            }

            //Set the text elements
            buildTitleOverColor(holder);
            buildSubTitleOverColor(holder);

            //Set the second half area
            if (viewStub != null) {
                setupInnerViewElementsSecondHalf(viewStub);
            }

        }
    }

    static class ViewHolder {

        /**
         * LinearLayout
         */
        DirectionalLayout mLinearLayoutContainer;

        /**
         * Title
         */
        private Text mTitleView;

        /**
         * SubTitle
         */
        private Text mSubTitleView;

        /**
         * ViewStub for the secondHalf card
         */
        private Component mSecondHalfView;

    }

    protected void setupInnerViewElementsSecondHalf(Component secondHalfView) {

        if (mOnSetupInnerElements != null)
            mOnSetupInnerElements.setupInnerViewElementsSecondHalf(secondHalfView);

    }

    ;


    /**
     * Build the title to be applied over the color
     * @param viewHolder viewHolder
     */
    protected void buildTitleOverColor(ViewHolder viewHolder) {

        if (viewHolder != null && viewHolder.mTitleView != null) {
            if (mTitleOverColorResId != 0) {
                if (mContext != null) {
                    try {
                        viewHolder.mTitleView.setText(mContext.getResourceManager().getElement(mTitleOverColorResId).getString());
                    } catch (Exception e) {
                        HiLog.error(Constants.LABEL_LOG, e.toString());
                    }
                }
            } else {
                viewHolder.mTitleView.setText(mTitleOverColor.toString());
            }
        }
    }

    /**
     * Build the subtitle to be applied over the color
     * @param viewHolder viewHolder
     */
    protected void buildSubTitleOverColor(ViewHolder viewHolder) {

        if (viewHolder != null && viewHolder.mSubTitleView != null) {
            if (mSubTitleOverColorResId != 0) {
                viewHolder.mSubTitleView.setVisibility(VISIBLE);
                if (mContext != null) {
                    try {
                        viewHolder.mSubTitleView.setText(mContext.getResourceManager().getElement(mSubTitleOverColorResId).getString());
                    } catch (Exception e) {
                        HiLog.error(Constants.LABEL_LOG, e.toString());
                    }
                }
            } else if (mSubTitleOverColor != null) {
                viewHolder.mSubTitleView.setText(mSubTitleOverColor.toString());
                viewHolder.mSubTitleView.setVisibility(VISIBLE);
            } else {
                viewHolder.mSubTitleView.setVisibility(HIDE);
            }
        }
    }

    // -------------------------------------------------------------
    // Getters and setters
    // -------------------------------------------------------------


    public int getColorResourceId() {
        return mColorResourceId;
    }

    public void setColorResourceId(int colorResourceId) {
        mColorResourceId = colorResourceId;
    }

    public CharSequence getTitleOverColor() {
        return mTitleOverColor;
    }

    public void setTitleOverColor(CharSequence titleOverColor) {
        mTitleOverColor = titleOverColor;
    }

    public int getTitleOverColorResId() {
        return mTitleOverColorResId;
    }

    public void setTitleOverColorResId(int titleOverColorResId) {
        mTitleOverColorResId = titleOverColorResId;
    }

    public CharSequence getSubTitleOverColor() {
        return mSubTitleOverColor;
    }

    public void setSubTitleOverColor(CharSequence subTitleOverColor) {
        mSubTitleOverColor = subTitleOverColor;
    }

    public int getSubTitleOverColorResId() {
        return mSubTitleOverColorResId;
    }

    public void setSubTitleOverColorResId(int subTitleOverColorResId) {
        mSubTitleOverColorResId = subTitleOverColorResId;
    }

    public void setSecondHalfViewStubLayoutId(int secondHalfViewStubLayoutId) {
        mSecondHalfViewStubLayoutId = secondHalfViewStubLayoutId;
    }


    public void setOnSetupInnerElements(OnSetupInnerElements onSetupInnerElements) {
        mOnSetupInnerElements = onSetupInnerElements;
    }
}
